/**************************************************
 * Serial.ino
 *
 * this sketch reads command on the serial port 
 * and controls Esplora output.
 * Available command are
 * HELP
 * ON
 * OFF
 * BLINK
 * BEEP
 *
 * Use Arduino IDE Serial Monitor, Hyperterm or 
 * putty to send commands to your board
 *
 * Created on 19 dicember 2013
 * By Stefano Ricci <stefano@ricciandco.it>
 ****************************************************/

#include <Esplora.h>
#include <String.h>

void setup() {
  Serial.begin(9600);     // initialize serial communication
}


#define BUFLEN 10    // maximum lenght of the imbut buffer (exceeding bytes are discarderd
byte buffer[BUFLEN+1];

void loop() {
  int i = 0;
  while (Serial.available() > 0) {
    int b = Serial.read();
    if (i<BUFLEN) {  // save till the maximum lenght
      buffer[i++] = b;
    }
  }
  buffer[i] = '\0';
  
  if (strcmp((char *)buffer,"ON") == 0) {
    Esplora.writeRGB(255,255,255);
  }
  if (strcmp((char *)buffer,"OFF") == 0) {
    Esplora.writeRGB(0,0,0);
  }
  if (strcmp((char *)buffer,"BLINK") == 0) {
    for (i=0; i<5; i++) {
      Esplora.writeRed(255);
      delay(50);
      Esplora.writeRed(0);
      delay(50);
    }
  }  
  if (strcmp((char *)buffer,"BEEP") == 0) {
    Esplora.tone(440,100);
  }    
  if (strcmp((char *)buffer,"HELP") == 0 || strcmp((char *)buffer, "?") == 0) {
    Serial.println("== HELP ===========");
    Serial.println("ON: switch on the led");
    Serial.println("OFF: switch off the led");    
    Serial.println("BLINK: the led blinks"); 
    Serial.println("BEEP: bip!");   
  }
 
}


